<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>png++: consumer.hpp Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">png++
   &#160;<span id="projectnumber">0.2.1</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">consumer.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="consumer_8hpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * Copyright (C) 2007,2008   Alex Shulgin</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * This file is part of png++ the C++ wrapper for libpng.  PNG++ is free</span>
<a name="l00005"></a>00005 <span class="comment"> * software; the exact copying conditions are as follows:</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
<a name="l00008"></a>00008 <span class="comment"> * modification, are permitted provided that the following conditions are met:</span>
<a name="l00009"></a>00009 <span class="comment"> *</span>
<a name="l00010"></a>00010 <span class="comment"> * 1. Redistributions of source code must retain the above copyright notice,</span>
<a name="l00011"></a>00011 <span class="comment"> * this list of conditions and the following disclaimer.</span>
<a name="l00012"></a>00012 <span class="comment"> *</span>
<a name="l00013"></a>00013 <span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span>
<a name="l00014"></a>00014 <span class="comment"> * notice, this list of conditions and the following disclaimer in the</span>
<a name="l00015"></a>00015 <span class="comment"> * documentation and/or other materials provided with the distribution.</span>
<a name="l00016"></a>00016 <span class="comment"> *</span>
<a name="l00017"></a>00017 <span class="comment"> * 3. The name of the author may not be used to endorse or promote products</span>
<a name="l00018"></a>00018 <span class="comment"> * derived from this software without specific prior written permission.</span>
<a name="l00019"></a>00019 <span class="comment"> *</span>
<a name="l00020"></a>00020 <span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS&#39;&#39; AND ANY EXPRESS OR</span>
<a name="l00021"></a>00021 <span class="comment"> * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES</span>
<a name="l00022"></a>00022 <span class="comment"> * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN</span>
<a name="l00023"></a>00023 <span class="comment"> * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<a name="l00024"></a>00024 <span class="comment"> * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED</span>
<a name="l00025"></a>00025 <span class="comment"> * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR</span>
<a name="l00026"></a>00026 <span class="comment"> * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF</span>
<a name="l00027"></a>00027 <span class="comment"> * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING</span>
<a name="l00028"></a>00028 <span class="comment"> * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS</span>
<a name="l00029"></a>00029 <span class="comment"> * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<a name="l00030"></a>00030 <span class="comment"> */</span>
<a name="l00031"></a>00031 <span class="preprocessor">#ifndef PNGPP_CONSUMER_HPP_INCLUDED</span>
<a name="l00032"></a>00032 <span class="preprocessor"></span><span class="preprocessor">#define PNGPP_CONSUMER_HPP_INCLUDED</span>
<a name="l00033"></a>00033 <span class="preprocessor"></span>
<a name="l00034"></a>00034 <span class="preprocessor">#include &lt;cassert&gt;</span>
<a name="l00035"></a>00035 <span class="preprocessor">#include &lt;stdexcept&gt;</span>
<a name="l00036"></a>00036 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00037"></a>00037 <span class="preprocessor">#include &lt;istream&gt;</span>
<a name="l00038"></a>00038 
<a name="l00039"></a>00039 <span class="preprocessor">#include &quot;<a class="code" href="config_8hpp.html">config.hpp</a>&quot;</span>
<a name="l00040"></a>00040 <span class="preprocessor">#include &quot;<a class="code" href="error_8hpp.html">error.hpp</a>&quot;</span>
<a name="l00041"></a>00041 <span class="preprocessor">#include &quot;<a class="code" href="streaming__base_8hpp.html">streaming_base.hpp</a>&quot;</span>
<a name="l00042"></a>00042 <span class="preprocessor">#include &quot;<a class="code" href="reader_8hpp.html">reader.hpp</a>&quot;</span>
<a name="l00043"></a>00043 <span class="preprocessor">#include &quot;<a class="code" href="pixel__buffer_8hpp.html">pixel_buffer.hpp</a>&quot;</span>
<a name="l00044"></a>00044 
<a name="l00045"></a>00045 <span class="keyword">namespace </span>png
<a name="l00046"></a>00046 {
<a name="l00047"></a>00047 
<a name="l00121"></a>00121     <span class="keyword">template</span>&lt; <span class="keyword">typename</span> pixel,
<a name="l00122"></a>00122               <span class="keyword">class </span>pixcon,
<a name="l00123"></a>00123               <span class="keyword">class </span>info_holder = def_image_info_holder,
<a name="l00124"></a>00124               <span class="keywordtype">bool</span> interlacing_supported = <span class="keyword">false</span> &gt;
<a name="l00125"></a><a class="code" href="classpng_1_1consumer.html">00125</a>     <span class="keyword">class </span><a class="code" href="classpng_1_1consumer.html" title="Pixel consumer class template.">consumer</a>
<a name="l00126"></a>00126         : <span class="keyword">public</span> <a class="code" href="classpng_1_1streaming__base.html" title="A base class template for consumer and generator classes. Provides default reset() method implementat...">streaming_base</a>&lt; pixel, info_holder &gt;
<a name="l00127"></a>00127     {
<a name="l00128"></a>00128     <span class="keyword">public</span>:
<a name="l00129"></a><a class="code" href="classpng_1_1consumer.html#a6da1ca61bdd7bf813fedc88bd54956b6">00129</a>         <span class="keyword">typedef</span> pixel_traits&lt; pixel &gt; <a class="code" href="classpng_1_1consumer.html#a6da1ca61bdd7bf813fedc88bd54956b6">traits</a>;
<a name="l00130"></a>00130 
<a name="l00134"></a><a class="code" href="structpng_1_1consumer_1_1transform__identity.html">00134</a>         <span class="keyword">struct </span><a class="code" href="structpng_1_1consumer_1_1transform__identity.html" title="The default io transformation: does nothing.">transform_identity</a>
<a name="l00135"></a>00135         {
<a name="l00136"></a><a class="code" href="structpng_1_1consumer_1_1transform__identity.html#af5fd4907a114e6f5716b260d3dd7a820">00136</a>             <span class="keywordtype">void</span> <a class="code" href="structpng_1_1consumer_1_1transform__identity.html#af5fd4907a114e6f5716b260d3dd7a820">operator()</a>(<a class="code" href="classpng_1_1io__base.html" title="Base class for PNG reader/writer classes.">io_base</a>&amp;)<span class="keyword"> const </span>{}
<a name="l00137"></a>00137         };
<a name="l00138"></a>00138 
<a name="l00143"></a>00143         <span class="keyword">template</span>&lt; <span class="keyword">typename</span> istream &gt;
<a name="l00144"></a><a class="code" href="classpng_1_1consumer.html#a56c7e1c6a14e7ff253b5416bf8ea9f06">00144</a>         <span class="keywordtype">void</span> <a class="code" href="classpng_1_1consumer.html#a56c7e1c6a14e7ff253b5416bf8ea9f06" title="Reads an image from the stream using default io transformation.">read</a>(istream&amp; stream)
<a name="l00145"></a>00145         {
<a name="l00146"></a>00146             <a class="code" href="classpng_1_1consumer.html#a56c7e1c6a14e7ff253b5416bf8ea9f06" title="Reads an image from the stream using default io transformation.">read</a>(stream, transform_identity());
<a name="l00147"></a>00147         }
<a name="l00148"></a>00148 
<a name="l00157"></a>00157         <span class="keyword">template</span>&lt; <span class="keyword">typename</span> istream, <span class="keyword">class</span> transformation &gt;
<a name="l00158"></a><a class="code" href="classpng_1_1consumer.html#a09c64f854a6bc39ec63dfa157958dc9b">00158</a>         <span class="keywordtype">void</span> <a class="code" href="classpng_1_1consumer.html#a56c7e1c6a14e7ff253b5416bf8ea9f06" title="Reads an image from the stream using default io transformation.">read</a>(istream&amp; stream, transformation <span class="keyword">const</span>&amp; transform)
<a name="l00159"></a>00159         {
<a name="l00160"></a>00160             <a class="code" href="classpng_1_1reader.html" title="The PNG reader class template. This is the low-level reading interface--use image class or consumer c...">reader&lt; istream &gt;</a> rd(stream);
<a name="l00161"></a>00161             rd.<a class="code" href="classpng_1_1reader.html#a5dc827c5e9a113f8821c1ff08b2ab111" title="Reads info about PNG image.">read_info</a>();
<a name="l00162"></a>00162             transform(rd);
<a name="l00163"></a>00163 
<a name="l00164"></a>00164 <span class="preprocessor">#if __BYTE_ORDER == __LITTLE_ENDIAN</span>
<a name="l00165"></a>00165 <span class="preprocessor"></span>            <span class="keywordflow">if</span> (pixel_traits&lt; pixel &gt;::get_bit_depth() == 16)
<a name="l00166"></a>00166             {
<a name="l00167"></a>00167 <span class="preprocessor">#ifdef PNG_READ_SWAP_SUPPORTED</span>
<a name="l00168"></a>00168 <span class="preprocessor"></span>                rd.<a class="code" href="classpng_1_1io__base.html#ae937edb8117961235667f808a3f88e3e">set_swap</a>();
<a name="l00169"></a>00169 <span class="preprocessor">#else</span>
<a name="l00170"></a>00170 <span class="preprocessor"></span>                <span class="keywordflow">throw</span> <a class="code" href="classpng_1_1error.html" title="Exception class to represent runtime errors related to png++ operation.">error</a>(<span class="stringliteral">&quot;Cannot read 16-bit image:&quot;</span>
<a name="l00171"></a>00171                             <span class="stringliteral">&quot; recompile with PNG_READ_SWAP_SUPPORTED.&quot;</span>);
<a name="l00172"></a>00172 <span class="preprocessor">#endif</span>
<a name="l00173"></a>00173 <span class="preprocessor"></span>            }
<a name="l00174"></a>00174 <span class="preprocessor">#endif</span>
<a name="l00175"></a>00175 <span class="preprocessor"></span>
<a name="l00176"></a>00176             <span class="comment">// interlace handling _must_ be set up prior to info update</span>
<a name="l00177"></a>00177             <span class="keywordtype">size_t</span> pass_count;
<a name="l00178"></a>00178             <span class="keywordflow">if</span> (rd.<a class="code" href="classpng_1_1io__base.html#aafcad747360e4cfbb942e6a9cbe3e8d2">get_interlace_type</a>() != <a class="code" href="namespacepng.html#a4b468278362e4ccf0b2c52f9c0692bdca173491c30047b28a7641cba46d97331a">interlace_none</a>)
<a name="l00179"></a>00179             {
<a name="l00180"></a>00180 <span class="preprocessor">#ifdef PNG_READ_INTERLACING_SUPPORTED</span>
<a name="l00181"></a>00181 <span class="preprocessor"></span>                pass_count = rd.<a class="code" href="classpng_1_1io__base.html#acdb6c2fe9e68f6ce2643131a21999279">set_interlace_handling</a>();
<a name="l00182"></a>00182 <span class="preprocessor">#else</span>
<a name="l00183"></a>00183 <span class="preprocessor"></span>                <span class="keywordflow">throw</span> <a class="code" href="classpng_1_1error.html" title="Exception class to represent runtime errors related to png++ operation.">error</a>(<span class="stringliteral">&quot;Cannot read interlaced image:&quot;</span>
<a name="l00184"></a>00184                             <span class="stringliteral">&quot; interlace handling disabled.&quot;</span>);
<a name="l00185"></a>00185 <span class="preprocessor">#endif</span>
<a name="l00186"></a>00186 <span class="preprocessor"></span>            }
<a name="l00187"></a>00187             <span class="keywordflow">else</span>
<a name="l00188"></a>00188             {
<a name="l00189"></a>00189                 pass_count = 1;
<a name="l00190"></a>00190             }
<a name="l00191"></a>00191 
<a name="l00192"></a>00192             rd.<a class="code" href="classpng_1_1reader.html#ae38d4ce06488e1addb6e5ff0c8e54a58">update_info</a>();
<a name="l00193"></a>00193             <span class="keywordflow">if</span> (rd.<a class="code" href="classpng_1_1io__base.html#ab8070ca679dfb49c226978297e7b4c08">get_color_type</a>() != traits::get_color_type()
<a name="l00194"></a>00194                 || rd.<a class="code" href="classpng_1_1io__base.html#a391152531f1825e81460b9847a3a8000">get_bit_depth</a>() != traits::get_bit_depth())
<a name="l00195"></a>00195             {
<a name="l00196"></a>00196                 <span class="keywordflow">throw</span> std::logic_error(<span class="stringliteral">&quot;color type and/or bit depth mismatch&quot;</span>
<a name="l00197"></a>00197                                        <span class="stringliteral">&quot; in png::consumer::read()&quot;</span>);
<a name="l00198"></a>00198             }
<a name="l00199"></a>00199 
<a name="l00200"></a>00200             this-&gt;<a class="code" href="classpng_1_1streaming__base.html#adab066b598745083ed1031f20f702faf">get_info</a>() = rd.<a class="code" href="classpng_1_1io__base.html#a581297639a9102527d09ab868c16915b">get_image_info</a>();
<a name="l00201"></a>00201 
<a name="l00202"></a>00202             pixcon* pixel_con = <span class="keyword">static_cast&lt;</span> pixcon* <span class="keyword">&gt;</span>(<span class="keyword">this</span>);
<a name="l00203"></a>00203             <span class="keywordflow">if</span> (pass_count &gt; 1 &amp;&amp; !interlacing_supported)
<a name="l00204"></a>00204             {
<a name="l00205"></a>00205                 skip_interlaced_rows(rd, pass_count);
<a name="l00206"></a>00206                 pass_count = 1;
<a name="l00207"></a>00207             }
<a name="l00208"></a>00208             read_rows(rd, pass_count, pixel_con);
<a name="l00209"></a>00209 
<a name="l00210"></a>00210             rd.<a class="code" href="classpng_1_1reader.html#a198c8345b97ddfac9f2f7474f196f45c" title="Reads ending info about PNG image.">read_end_info</a>();
<a name="l00211"></a>00211         }
<a name="l00212"></a>00212 
<a name="l00213"></a>00213     <span class="keyword">protected</span>:
<a name="l00214"></a><a class="code" href="classpng_1_1consumer.html#ab08a4f9206fc3cd535a7821e7f2eeeba">00214</a>         <span class="keyword">typedef</span> <a class="code" href="classpng_1_1streaming__base.html" title="A base class template for consumer and generator classes. Provides default reset() method implementat...">streaming_base&lt; pixel, info_holder &gt;</a> <a class="code" href="classpng_1_1consumer.html#ab08a4f9206fc3cd535a7821e7f2eeeba">base</a>;
<a name="l00215"></a>00215 
<a name="l00220"></a><a class="code" href="classpng_1_1consumer.html#a17941cccb5fe7101399b8bda61cac7db">00220</a>         <span class="keyword">explicit</span> <a class="code" href="classpng_1_1consumer.html#a17941cccb5fe7101399b8bda61cac7db" title="Constructs a consumer object using passed image_info object to store image information.">consumer</a>(<a class="code" href="classpng_1_1image__info.html" title="Holds information about PNG image.">image_info</a>&amp; <a class="code" href="classpng_1_1info.html" title="Holds information about PNG image. Adapter class for IO image operations.">info</a>)
<a name="l00221"></a>00221             : <a class="code" href="classpng_1_1streaming__base.html">base</a>(info)
<a name="l00222"></a>00222         {
<a name="l00223"></a>00223         }
<a name="l00224"></a>00224 
<a name="l00225"></a>00225     <span class="keyword">private</span>:
<a name="l00226"></a>00226         <span class="keyword">template</span>&lt; <span class="keyword">typename</span> istream &gt;
<a name="l00227"></a>00227         <span class="keywordtype">void</span> skip_interlaced_rows(<a class="code" href="classpng_1_1reader.html" title="The PNG reader class template. This is the low-level reading interface--use image class or consumer c...">reader&lt; istream &gt;</a>&amp; rd, <span class="keywordtype">size_t</span> pass_count)
<a name="l00228"></a>00228         {
<a name="l00229"></a>00229             <span class="keyword">typedef</span> std::vector&lt; pixel &gt; row;
<a name="l00230"></a>00230             <span class="keyword">typedef</span> row_traits&lt; row &gt; row_traits_type;
<a name="l00231"></a>00231             row dummy_row(this-&gt;<a class="code" href="classpng_1_1streaming__base.html#adab066b598745083ed1031f20f702faf">get_info</a>().get_width());
<a name="l00232"></a>00232             <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> pass = 1; pass &lt; pass_count; ++pass)
<a name="l00233"></a>00233             {
<a name="l00234"></a>00234                 rd.<a class="code" href="classpng_1_1reader.html#a07f8235aab8059260b4857feba579ccb" title="Reads a row of image data at a time.">read_row</a>(reinterpret_cast&lt; byte* &gt;
<a name="l00235"></a>00235                             (row_traits_type::get_data(dummy_row)));
<a name="l00236"></a>00236             }
<a name="l00237"></a>00237         }
<a name="l00238"></a>00238 
<a name="l00239"></a>00239         <span class="keyword">template</span>&lt; <span class="keyword">typename</span> istream &gt;
<a name="l00240"></a>00240         <span class="keywordtype">void</span> read_rows(reader&lt; istream &gt;&amp; rd, <span class="keywordtype">size_t</span> pass_count,
<a name="l00241"></a>00241                        pixcon* pixel_con)
<a name="l00242"></a>00242         {
<a name="l00243"></a>00243             <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> pass = 0; pass &lt; pass_count; ++pass)
<a name="l00244"></a>00244             {
<a name="l00245"></a>00245                 pixel_con-&gt;reset(pass);
<a name="l00246"></a>00246 
<a name="l00247"></a>00247                 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> pos = 0; pos &lt; this-&gt;<a class="code" href="classpng_1_1streaming__base.html#adab066b598745083ed1031f20f702faf">get_info</a>().<a class="code" href="classpng_1_1image__info.html#a640f6451351d62920fa2786dea53cd4d">get_height</a>(); ++pos)
<a name="l00248"></a>00248                 {
<a name="l00249"></a>00249                     rd.read_row(pixel_con-&gt;get_next_row(pos));
<a name="l00250"></a>00250                 }
<a name="l00251"></a>00251             }
<a name="l00252"></a>00252         }
<a name="l00253"></a>00253     };
<a name="l00254"></a>00254 
<a name="l00255"></a>00255 } <span class="comment">// namespace png</span>
<a name="l00256"></a>00256 
<a name="l00257"></a>00257 <span class="preprocessor">#endif // PNGPP_CONSUMER_HPP_INCLUDED</span>
</pre></div></div><!-- contents -->


<hr class="footer"/><address class="footer"><small>
Generated on Fri Jan 25 2013 14:10:10 for png++ by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
